//
//  GRIDCardCertificationViewController.m
//  YC_GRice
//
//  Created by LiuFeng on 2018/7/23.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRIDCardCertificationViewController.h"
#import "GRBankCertificationViewController.h"

#import "GRCertificationHeader.h"
#import "GRCertificationFooter.h"
#import "GRCertificationViewCell.h"
#import "GRSuccessViewController.h"

@interface GRIDCardCertificationViewController () <GRCertificationProtocol>

{
    NSArray *titleArray;
    NSArray *identifyKeys;
}

@property (nonatomic, strong) NSMutableDictionary *resultDic;
@property (nonatomic, strong) NSMutableDictionary *paramers; // 进入银行卡认证参数
@property (nonatomic, strong) GRCertificationFooter *footer;

@end

@implementation GRIDCardCertificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    titleArray      = @[@"真实姓名", @"身份证号", @"有效期"];
    identifyKeys    = @[@"name", @"idCardNumber", @"validityTime"];
    // Uncomment the following line to preserve selection between presentations.
    GRCertificationHeader *header  = [[GRCertificationHeader alloc] initWithCertification:GRCertificationIdCard];
    header.delegate                = self;
    header.currentVc               = self;
    self.tableView.tableHeaderView = header;
    
    self.tableView.tableFooterView = self.footer;
    
    self.tableView.rowHeight       = 45;
    self.tableView.backgroundColor = HEXCOLOR(0xf8f8f8);
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    [self.tableView registerNib:[UINib nibWithNibName:@"GRCertificationViewCell" bundle:nil] forCellReuseIdentifier:@"GRCertificationViewCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return titleArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GRCertificationViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"GRCertificationViewCell" forIndexPath:indexPath];
    cell.titleLabel.text           = titleArray[indexPath.row];
    NSString *key                  = identifyKeys[indexPath.row];
    cell.infoTextField.delegate    = self;
    if (self.resultDic[key]) {
        cell.infoTextField.text    = self.resultDic[key];
    }else {
        cell.infoTextField.placeholder = @"拍照识别";
    }
    return cell;
}


#pragma Mark  =================  GRCertificationProtocol  ==================

-(void)didselectConfirmAction:(UIButton *)sender {
    
    NSMutableDictionary *paramar = [NSMutableDictionary dictionary];
    for (NSDictionary *dic in self.paramers.allValues) {
        for (NSString *key in dic.allKeys) {
            if (NSStringNotNULL(dic[key])) {
                [paramar setValue:dic[key] forKey:key];
            }
        }
    }
    
    for (int i = 0; i < titleArray.count; i++) {
        GRCertificationViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        [paramar setValue:cell.infoTextField.text forKey:identifyKeys[i]];
    }
 
    [GRCertificationAction submitPersonalCertification:paramar completionHandler:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
       
                GRSuccessViewController * successVC = [[GRSuccessViewController alloc]init];
                [self.navigationController pushViewController:successVC animated:YES];
                GRBasisKeyModel *basisModel = [GRUserBasisInfo UserBasis];
                basisModel.appraisal = YES;
                [[GRUserBasisInfo shareBasis] saveBasisKey:basisModel];
        
        }else {
            [GRHUD showErrorMsg:[error errorMsg]];
        }
        sender.enabled = YES;
    }];
 
}

/**
 获取身份证信息

 @param response <#response description#>
 */
-(void)getIdentificationInfomation:(NSDictionary *)response {
    NSString *validityTime = response[@"validityTime"];
    if (NSStringNotNULL(validityTime) && validityTime.length > 0) {
        [self.resultDic setValue:response[@"validityTime"] forKey:@"validityTime"];
    }else {
        if (NSStringNotNULL(response[@"name"])) {
            [self.resultDic setValue:response[@"name"] forKey:@"name"];
        }
        if (NSStringNotNULL(response[@"idCardNumber"])) {
            [self.resultDic setValue:response[@"idCardNumber"] forKey:@"idCardNumber"];
        }
    }

    // 此处判断 是因代理传值时分辨了正反面
    if (response[@"idCard"]) {
        [self.paramers setValue:response forKey:@"front"];
    }
    if (response[@"idCardBack"]){
        [self.paramers setValue:response forKey:@"back"];
    }
    
    if (self.resultDic.allKeys.count == 3) {
        [self changeNextBtnStatus:YES];
    }
    [self.tableView reloadData];
}

// 改变底部按钮
- (void)changeNextBtnStatus:(BOOL)next {
    _footer.sureBtn.enabled = next;
    _footer.sureBtn.backgroundColor = next ? KGMTHEM_COLOR : HEXCOLOR(0xeeeeee);
    [_footer.sureBtn setTitleColor:next ? KGMBLACK_COLOR : HEXCOLOR(0x999999) forState:UIControlStateNormal];
}

/**
 懒加载

 @return <#return value description#>
 */
-(NSMutableDictionary *)resultDic {
    if (!_resultDic) {
        _resultDic = [NSMutableDictionary dictionary];
    }
    return _resultDic;
}

-(NSMutableDictionary *)paramers {
    if (!_paramers) {
        _paramers = [NSMutableDictionary dictionary];
    }
    return _paramers;
}

-(GRCertificationFooter *)footer {
    if (!_footer) {
        _footer = [[GRCertificationFooter alloc] initWithFrame:CGRectMake(0, 0, KscreenWidth, 65)];
        _footer.delegate                = self;
        [self changeNextBtnStatus:NO];
        [_footer.sureBtn setTitle:@"提交" forState:UIControlStateNormal];
    }
    return _footer;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
